#region
/* Adoor.NET - Object/Relational mapping framework leveraging ADO.NET`s powerful disconnected mode.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *  
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *  
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */ 
#endregion

using System;
using System.Collections.Generic;

namespace Adoor.Object.Domain
{
	/// <summary>
	/// A collection of <see cref="EntityData"/> objects.
	/// </summary>
	public class EntityDataCollection
	{
		public EntityDataCollection(ObjectDomain domain)
		{
			this.domain = domain;
		}

		public EntityData this[Type type]
		{
			get
			{
               
				EntityData result;
                if(this.factoriesByType.TryGetValue(type, out result))
				    return result;

				foreach(ObjectDomain d in this.domain.References)
				{
					result = d.EntityInfos[type];
					if (result != null)
						return result;
				}
				return null;
			}
		}

		public EntityData this[string name]
		{
			get
			{
                EntityData result;
                if (this.factoriesByName.TryGetValue(name, out result))
                    return result;

				foreach(ObjectDomain d in this.domain.References)
				{
					result = d.EntityInfos[name];
					if (result != null)
						return result;
				}
				return null;
			}
		}

		public EntityData FindByOID(string id)
		{
			return FindByOID(id, new List<ObjectDomain>());
		}

		public EntityData FindByOID(string id, List<ObjectDomain> listAlreadyChecked)
		{
			listAlreadyChecked.Add(this.domain);
			foreach(EntityData entityData in this.factoriesByType.Values)
			{
				EntityData data = entityData.GetActualEntityData(id, false);
				if(data != null)
					return data;
			}
			
			foreach(ObjectDomain d in this.domain.References)
			{
				if(!listAlreadyChecked.Contains(d))
				{
					EntityData result = d.EntityInfos.FindByOID(id, listAlreadyChecked);
					if (result != null)
						return result;
				}
			}
			return null;
		}

		public void Add(Type type, EntityData factory)
		{
			this.factoriesByType.Add(type, factory);
			this.factoriesByName.Add(type.Name, factory);
		}

		public IEnumerator<EntityData> GetEnumerator()
		{
			return factoriesByType.Values.GetEnumerator();
		}

        private IDictionary<Type, EntityData> factoriesByType = new Dictionary<Type, EntityData>();
        private IDictionary<string, EntityData> factoriesByName = new Dictionary<string, EntityData>(StringComparer.CurrentCultureIgnoreCase);

        public IDictionary<Type, EntityData> FactoriesByType
        {
            get { return factoriesByType; }
        }

        public IDictionary<string, EntityData> FactoriesByName
        {
            get { return factoriesByName; }
        }

		private ObjectDomain domain;
	}
}
